package com.example.qxfw.qxxx.service.impl;

import com.example.qxfw.common.entity.qx.MsgMediumSmallScale;
import com.example.qxfw.common.entity.zy.RainWaterShed;
import com.example.qxfw.common.entity.zy.RiverBasin;
import com.example.qxfw.common.mapper.ContourSettingMapperXml;
import com.example.qxfw.common.mapper.MsgmediumsmallscaleMapperXml;
import com.example.qxfw.common.mapper.RainWaterShedMapperXml;
import com.example.qxfw.common.util.DateUtil;
import com.example.qxfw.qxxx.mapper.RiverBasinMapper;
import com.example.qxfw.qxxx.service.RegionalAutomaticStationService;
import com.example.qxfw.qxxx.vo.RiverBasinVo;
import com.example.qxfw.ybzz.mapper.MsgMediumSmallScaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.example.qxfw.common.util.StringUtil.toNull;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/8/16  星期三   13:47
 * @description : 此类用来
 ***************************************/
@Service
public class RegionalAutomaticStationServiceImpl implements RegionalAutomaticStationService {

    @Autowired
    private MsgmediumsmallscaleMapperXml msgmediumsmallscaleMapperXml;
    @Autowired
    private RiverBasinMapper riverBasinMapper;
    @Autowired
    private MsgMediumSmallScaleMapper msgMediumSmallScaleMapper;

    @Autowired
    private RainWaterShedMapperXml rainWaterShedMapperXml;

    @Autowired
    private ContourSettingMapperXml contourSettingMapperXml;

    @Override
    public List<MsgMediumSmallScale> queryList(final Timestamp pdtStart, final Timestamp pdtEnd, final String m_strSelDbName1,final String m_strSelDbName2, final String city, final String stationId, final String orderBy) {

        return msgmediumsmallscaleMapperXml.queryFactor(pdtStart, pdtEnd, m_strSelDbName1, m_strSelDbName2,city, stationId, orderBy);
    }

    @Override
    public List<MsgMediumSmallScale> queryTest(final String stationId, final String observTime, final Integer rain) {
        return msgmediumsmallscaleMapperXml.queryTest(stationId, observTime, rain);
    }

    @Override
    public List<RiverBasinVo> getWatershed() {
        List<RiverBasin> list = riverBasinMapper.selectList(null);
        List<RiverBasinVo> vos = new ArrayList<>();
        if (list.size()>0) {
            for (RiverBasin riverBasin : list) {
                String[] split = riverBasin.getCoordinate().split(";");
                double[][] arr = new double[split.length][2];
                for (int i = 0; i < split.length; i++) {
                    String[] split1 = split[i].split(",");
                    arr[i][0] = Double.parseDouble(split1[0]);
                    arr[i][1] = Double.parseDouble(split1[1]);
                }
                vos.add(new RiverBasinVo(riverBasin.getId(), riverBasin.getName(), arr, riverBasin.getCenterpointx(), riverBasin.getCenterpointx(), riverBasin.getMonit_threshold()));
            }
            return vos;
        }
        return vos;
    }

    @Override
    public List<MsgMediumSmallScale> getStatisticalChart(String county, String stationName, String date1, String date2, String time1, String time2, String element) {
        List<MsgMediumSmallScale> msgMediumSmallScaleList = new ArrayList<>();
        // 处理时间格式
//        Timestamp start = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date1 + " " + time1 + ":00:00"));
//        Timestamp end = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date2 + " " + time2 + ":00:00"));
        Timestamp start = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date1));
        Timestamp end = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date2));
        String type = "rain";
        if (element.equals("雨量")){
            type = "rain";
        }else if (element.equals("相对湿度")){
            type = "relhumidity";
        }else if (element.equals("温度")){
            type = "drybulbtemp";
        }else if (element.equals("最高温度")){
            type = "maxtemp";
        }else if (element.equals("最低温度")){
            type = "mintemp";
        }else if (element.equals("露点温度")){
            type = "dewtemp";
        }else if (element.equals("风速")){
            type = "windvelocity";
        }else if (element.equals("风向")){
            type = "winddirect";
        }else if (element.equals("最大风速")){
            type = "maxwindv";
        }else if (element.equals("最大风风向")){
            type = "maxwindd";
        }else if (element.equals("气压")){
            type = "stationpress";
        }else if (element.equals("最大气压")){
            type = "maxstationpress";
        }

        List<MsgMediumSmallScale> statisticalChartList = msgmediumsmallscaleMapperXml.selectSingleElementStatisticalChart(county, stationName, start, end, type);
        statisticalChartList.forEach(statisticalChart->{
            statisticalChart.setRain(toNull(statisticalChart.getRain()));
            statisticalChart.setRelhumidity(toNull(statisticalChart.getRelhumidity()));
            statisticalChart.setDrybulbtemp(toNull(statisticalChart.getDrybulbtemp()));
            statisticalChart.setMaxtemp(toNull(statisticalChart.getMaxtemp()));
            statisticalChart.setMintemp(toNull(statisticalChart.getMintemp()));
            statisticalChart.setDewtemp(toNull(statisticalChart.getDewtemp()));
            statisticalChart.setWindvelocity(toNull(statisticalChart.getWindvelocity()));
            statisticalChart.setWinddirect(toNull(statisticalChart.getWinddirect()));
            statisticalChart.setMaxwindv(toNull(statisticalChart.getMaxwindv()));
            statisticalChart.setMaxwindd(toNull(statisticalChart.getMaxwindd()));
            statisticalChart.setStationpress(toNull(statisticalChart.getStationpress()));
            statisticalChart.setMaxstationpress(toNull(statisticalChart.getMaxstationpress()));
            msgMediumSmallScaleList.add(statisticalChart);
        });
        return msgMediumSmallScaleList;
    }

    @Override
    public List<RainWaterShed> getRiverBasinFromRainWater() {
        return rainWaterShedMapperXml.getRainWaterShedByDrainagearea();
    }

    @Override
    public List<RainWaterShed> getRainWaterShedByName(final String name) {
        return rainWaterShedMapperXml.getRainWaterShedByName(name);
    }

    @Override
    public int selectByElementTypeAndValue( String elementType,  float elementValue) {
        return contourSettingMapperXml.selectByElementTypeAndValue(elementType, elementValue);
    }

    @Override
    public List<MsgMediumSmallScale> getStatisticalChartDay(String county, String stationName, String date1, String date2, String time1, String time2, String element) {
        List<MsgMediumSmallScale> msgMediumSmallScaleList = new ArrayList<>();
        // 处理时间格式
//        Timestamp start = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date1 + " " + time1 + ":00:00"));
//        Timestamp end = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date2 + " " + time2 + ":00:00"));
        Timestamp start = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date1));
        Timestamp end = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(date2));
        String type = "sum(cast(cast(Rain as float) as numeric(18,1))) as rain";
        String smalltype = "rain";
        if (element.equals("雨量")){
            type = "cast(sum(cast(cast(Rain as float) as numeric(18,1))) as numeric(18,1)) as rain";
            smalltype = "rain";
        }else if (element.equals("相对湿度")){
            type = "cast(avg(relhumidity) as numeric(18,1)) as relhumidity";
            smalltype = "relhumidity";
        }else if (element.equals("温度")){
            type = "cast(avg(cast(cast(DryBulbTemp as float)  as numeric(18,1))) as numeric(18,1)) as DryBulbTemp";
            smalltype = "DryBulbTemp";
        }else if (element.equals("最高温度")){
            type = "cast(avg(cast(cast(MaxTemp as float) as numeric(18,1))) as numeric(18,1)) as MaxTemp";
            smalltype = "MaxTemp";
        }else if (element.equals("最低温度")){
            type = "cast(avg(cast(cast(MinTemp as float)  as numeric(18,1))) as numeric(18,1)) as MinTemp";
            smalltype = "MinTemp";
        }else if (element.equals("露点温度")){
            type = "cast(avg(cast(cast(DewTemp as float) as numeric(18,1))) as numeric(18,1)) as DewTemp";
            smalltype = "DewTemp";
        }else if (element.equals("风速")){
            type = "cast(avg(cast(cast(WindVelocity as float) as numeric(18,1))) as numeric(18,1)) as WindVelocity";
            smalltype = "WindVelocity";
        }else if (element.equals("风向")){
            type = "cast(avg(WindDirect) as numeric(18,1)) as WindDirect";
            smalltype = "WindDirect";
        }else if (element.equals("最大风速")){
            type = "cast(max(cast(cast(MaxWindV as float) as numeric(18,1))) as numeric(18,1)) as MaxWindV";
            smalltype = "MaxWindV";
        }else if (element.equals("最大风风向")){
            type = "cast(max(MaxWindD) as numeric(18,1)) as MaxWindD";
            smalltype = "MaxWindD";
        }else if (element.equals("气压")){
            type = "cast(avg(cast(cast(StationPress as float) as numeric(18,1))) as numeric(18,1)) as StationPress";
            smalltype = "StationPress";
        }else if (element.equals("最大气压")){
            type = "cast(max(cast(cast(MaxStationPress as float) as numeric(18,1))) as numeric(18,1)) as MaxStationPress";
            smalltype = "MaxStationPress";
        }

        List<MsgMediumSmallScale> statisticalChartList = msgmediumsmallscaleMapperXml.selectSingleElementStatisticalChartDay(county, stationName, start, end, type ,smalltype);
        statisticalChartList.forEach(statisticalChart->{
            statisticalChart.setRain(toNull(statisticalChart.getRain()));
            statisticalChart.setRelhumidity(toNull(statisticalChart.getRelhumidity()));
            statisticalChart.setDrybulbtemp(toNull(statisticalChart.getDrybulbtemp()));
            statisticalChart.setMaxtemp(toNull(statisticalChart.getMaxtemp()));
            statisticalChart.setMintemp(toNull(statisticalChart.getMintemp()));
            statisticalChart.setDewtemp(toNull(statisticalChart.getDewtemp()));
            statisticalChart.setWindvelocity(toNull(statisticalChart.getWindvelocity()));
            statisticalChart.setWinddirect(toNull(statisticalChart.getWinddirect()));
            statisticalChart.setMaxwindv(toNull(statisticalChart.getMaxwindv()));
            statisticalChart.setMaxwindd(toNull(statisticalChart.getMaxwindd()));
            statisticalChart.setStationpress(toNull(statisticalChart.getStationpress()));
            statisticalChart.setMaxstationpress(toNull(statisticalChart.getMaxstationpress()));
            msgMediumSmallScaleList.add(statisticalChart);
        });
        return msgMediumSmallScaleList;
    }
}
